// Vector.cpp //
#include "vector.h"

#include "math.h"
#ifndef M_PI
#define M_PI           3.14159265358979323846
#endif

//Vectors were used enough that making a dedicated class seemed useful

Vector::Vector(float x, float y)//constructor
{
	X = x;
	Y = y;
}

// trims vector down to a unit vector
void Vector::Normalize()
{
	float length = Length();
	if(length <= 0)
	    return;

	X /= length;
	Y /= length;
}

// finds the angle of a given vector (radians)
float Vector::Angle()
{
    if(X == 0.0f)
        if(Y > 0.0f)
			return M_PI / 2.0f;
		else
		    return 3.0f * M_PI / 2.0f;

    float omega = Y / X;

    if (omega < 0.0f)
		omega *= -1;

    float angle = atan(omega);

    if(X < 0.0f && Y >= 0.0f)
		return M_PI - angle;

    if(X < 0.0f && Y <= 0.0f)
		return M_PI + angle;

    if(X > 0.0f && Y <= 0.0f)
		return 2.0f * M_PI - angle;

    return angle;
}

// find magnitude
float Vector::Length()
{
	return sqrt(X * X + Y * Y);
}

// find a vector of the given angle
Vector Vector::CalculateDirection(float angle)
{
	Vector vector;
	
	vector.X = sin(angle);
    vector.Y = cos(angle);
	    
	return vector;
}

Vector Vector::operator +(const Vector& v)
{
	Vector vector;
	vector.X = X + v.X;
	vector.Y = Y + v.Y;

	return vector;
}

//allow for subtraction
Vector Vector::operator -(const Vector& v)
{
	Vector vector;
	vector.X = X - v.X;
	vector.Y = Y - v.Y;

	return vector;
}

//scaler multiplication
Vector Vector::operator *(const float& mag)
{
	Vector vector;
	vector.X = X * mag;
	vector.Y = Y * mag;

	return vector;
}

//scaler division
Vector Vector::operator /(const float& mag)
{
	Vector vector;
	vector.X = X / mag;
	vector.Y = Y / mag;

	return vector;
}

//assignment
void Vector::operator =(const Vector& v)
{
	X = v.X;
	Y = v.Y;
}

//plus assignment
void Vector::operator +=(const Vector& v)
{
	X += v.X;
	Y += v.Y;
}

//minus assignment
void Vector::operator -=(const Vector& v)
{
	X -= v.X;
	Y -= v.Y;
}

//vector multiplication
Vector operator *(const float& mag, const Vector& v)
{
	Vector vector;
	vector.X = v.X * mag;
	vector.Y = v.Y * mag;

	return vector;
}
